class Request {
    constructor({
        timeout = 60000,
        header = {},
        baseUrl = ''
    }) {
        this.timeout = timeout;
        this.baseUrl = baseUrl;
        this.header = header;
    }
    forMatUrl(url, params) {
        if (typeof params === 'object') {
            params = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
        }
        if (url.includes('?')) {
            return `${url}${url.endsWith('&') ? '' : "&"}${params}`
        }
        return `${url}${params ? '?' : ""}${params ? params : ""}`
    }
    get(url = '', params = {}, config = {}) {
        return this.sendRequest({
            url: this.forMatUrl(url, params),
            method: "GET",
            config
        })
    }
    post(url = '', data = {}, config = {}) {
        return this.sendRequest({
            url,
            method: "POST",
            data,
            config
        })
    }
    DELETE(url = '', data = {}, config = {}) {
        return this.sendRequest({
            url: this.forMatUrl(url, data),
            method: "DELETE",
            data,
            config
        })
    }
    sendRequest({
        config,
        ...option
    }) {
        const httpConfig = {
            ...option,
            url: `${(/^https?/).test(option.url) ? '' : this.baseUrl}${option.url}`,
            header: {
                ...this.header,
                ...(config.header || {})
            },
            timeout: this.timeout
        }
        return new Promise((resolve, reject) => {
            uni.request({
                ...httpConfig,
                success(res) {
                    resolve(res.data)
                },
                fail(error) {
                    reject(error)
                }
            })
        })
    }
}
const userinfo = uni.getStorageSync('Authorization');
export default new Request({
    timeout: 10000,
    baseUrl: "https://bjwz.bwie.com/mall4j",
    header: { //所有接口携带的公共header
        Authorization: userinfo ? `bearer${userinfo}` : null
    }
})